package com.kaoshi.management.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaoshi.common.config.Result;
import com.kaoshi.management.pojo.XtManagement;
import com.kaoshi.management.service.XtManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-29
 */
@RestController
@CrossOrigin
@RequestMapping("/management")
public class XtManagementController {

    @Autowired
    private XtManagementService xtManagementService;

    @PostMapping("/aa/{current}/{pageSize}")
    public Result findAll(@PathVariable Integer current,
                           @PathVariable Integer pageSize,
                          @RequestBody XtManagement xtManagement){
        Page<XtManagement> pageList=xtManagementService.getPageList(current,pageSize,xtManagement);

        return Result.ok(pageList);
    }

    @PostMapping("change")
    public Result change(@RequestBody XtManagement xtManagement){
        xtManagement.setPeople("李小明");
        if(xtManagement.getId() == null){
            LocalDate localDate= LocalDate.now();
            xtManagement.setStatus(0);
            xtManagement.setModificationtime(localDate.atStartOfDay());
            xtManagementService.save(xtManagement);
            return Result.ok("添加成功");
        }else {
            xtManagementService.updateById(xtManagement);
            return Result.ok("修改成功");
        }
    }
    @PostMapping("qh")
    public Result qh(@RequestBody XtManagement xtManagement) {
        XtManagement currentManagement = xtManagementService.getById(xtManagement.getId());
        if (currentManagement != null) {
            int currentStatus = currentManagement.getStatus();
            // 切换状态，示例：如果当前状态为 0 则切换为 1，反之亦然
            int newStatus = (currentStatus == 0) ? 1 : 0;
            currentManagement.setStatus(newStatus);
            currentManagement.setModificationtime(LocalDateTime.now()); // 设置修改时间

            xtManagementService.updateById(currentManagement);
        }
        return Result.ok("修改成功");
    }

    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Integer id){
        xtManagementService.removeById(id);
        return Result.ok();
    }

//    @DeleteMapping("delete/{id}")
//    public Result delete(@PathVariable Integer id) {
//        // 获取要删除的实体
//        XtManagement xtManagement = xtManagementService.getById(id);
//        if (xtManagement != null) {
//            // 设置为逻辑删除状态
//            xtManagement.setLjdelete(1);
//            xtManagementService.updateById(xtManagement); // 更新记录
//        }
//        return Result.ok("删除成功");
//    }


    @RequestMapping("getBq")
    public List<XtManagement> getBq(){
        List<XtManagement> bqs =xtManagementService.getBq();
        return bqs;
    }
}
